﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Linq.Expressions;
using ExtendPropertyLib;
using MaxZhang.EasyEntities.Persistence;
using MaxZhang.EasyEntities.Persistence.Mapping;
using MaxZhang.EasyEntities.Persistence.Provider;
using System.Data;
using MaxZhang.EasyEntities.Persistence.Provider.SQLServer;
using MaxZhang.EasyEntities.Persistence.EntityModel;
using System.Data.OracleClient;


namespace exptest
{
    class Program
    {
        

        public static int v2 { get { return 50; } }
        static void Main(string[] args)
        {



            //DbSession.AddMap("SQLMap1.xml");
            //var commandInfo = DbSession.Default.GetCommandInfo("GetAdminUserById");

            //DAL1 dal = new DAL1();
            //var data = dal.GetAdminUserById("0000000001", "maxzhang");

            var classInfos = new List<string>() { "0001", "0002" };


            using (var session = DbSession.Default)
            {

         

                Expression<Func<AdminUser1, bool>> wt1 = u =>u.CID =="0001" && u.NameA == "xt"&& (SQLMethod.IsNull(u.Age) || u.Age > 18);
                string wheret1 = TranslateExtendtion.TranslateConditional(wt1, session.Provider);

                decimal?[] ls = new decimal?[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                Expression<Func<SModel, bool>> w1 = m => ls.Contains(m.User.Age) ;
                string where1 = TranslateExtendtion.TranslateConditional(w1,session.Provider);
                
                Expression<Func<SModel, bool>> w = m => m.User.Age > 5 && m.D.Name == "mm" && m.Class.Name == "c1";

                string where = TranslateExtendtion.TranslateConditional(w, session.Provider);

                #region CDMQ

                string name = "ma1985";

                AdminUser1 user1 = new AdminUser1() { ID = "0004", CID = "1", NameA = "cc", Age = 29, Date = DateTime.Now , IsDel=false };

                //session.InsertTransaction(user1);
                //session.SubmitChanges();
                //session.Insert<AdminUser1>(u => new AdminUser1{
                //    ID = "0126",
                //    NameA = name,
                //    Date = SQLMethod.GetDate()
                //});

                //user1.Age = 5;
                //session.UpdateTransaction(user1);
                //session.SubmitChanges();


                //新的Update,Delete语句要执行Go才会执行。
                //新的Update语句
                session.Update<AdminUser1>(u => new AdminUser1{
                                                        NameA = name,
                                                        Date = SQLMethod.GetDate(),  
                                                    }).Where(p => p.ID == "0001");

                //新删除语句
                session.Delete<AdminUser1>().Where(u => u.ID == "5");

              
                #endregion


                #region 查询
                List<IDataParameter> parameters = new List<IDataParameter>();
                var p1 = new OracleParameter("userId",OracleType.VarChar,10);
                p1.Value ="0001";
                parameters.Add(p1);

                var p2 = new OracleParameter("RESULT", OracleType.Cursor);
                p2.Direction = ParameterDirection.Output;
                parameters.Add(p2);


                var ds1 = session.RunStoredProcedure("QUERYUSERTABLE", parameters.ToArray());

                var testMethodQuery = from u in session.CreateLinq<AdminUser1>()
                                      where u.Age > 18 && u.Date < SQLMethod.GetDate()
                                      select u;
                var testList = testMethodQuery.ToList();

                string log1 = session.Log;

                var t2 = session.CreateQuery<AdminUser1>().Where(u => u.Age > 18 && u.Date < SQLMethod.GetDate());
                string log2 = session.Log;



                var test1 = session.CreateQuery<AdminUser1>();
                var q = test1.Where(u => u.NameA.StartsWith("z")).Sum(u => u.Age);
                var dd1 =  q.ToInt();

                //var pagerTest = session.CreateQuery<AdminUser1>();

                //分页
                //var ddddd = pagerTest.Where(u => u.Age < v2).GetPager(1, 2).ToList();

                //应用查询对象
                var userQuery = session.CreateQuery<AdminUser1>();
                var c1 = session.CreateConditional<AdminUser1>(u => (u.Age >= 10 && u.Age <= 50) && 
                                                             (u.NameA.StartsWith("z") || u.NameA == "houxiaodong") &&
                                                             u.Date >= new DateTime(2013, 3, 28)) as QueryConditional<AdminUser1>;
              
                //返回特定列信息
                var userInfosv = userQuery.Where(c1).Select(a => new
                {
                    a.NameA,
                    a.Age,
                    a.Date,
                    a.IsDel
                }).ToDataSet();

                //var table1 = userInfosv.Tables[0];
                //var row = table1.NewRow();
                //row["ID"] = "0125";
                //row["Name"] = "mh";
                //row["Date"] = DateTime.Now;
                //row["Age"] = 18;
                //table1.Rows.Add(row);
                //更新dataset
                //session.UpdateDataSet<AdminUser1>(userInfosv);
          
                var cdc =
                    session.CreateConditional<AdminUser1, Class1, DList1>
                        ((u, c, d) => (u.Age > 10 && c.Name == "class1" || d.Name == "d1"));



                //Linq查询
                var users = session.CreateLinq<AdminUser1>();
                //查询对象在Linq查询中的应用
                var query = (from u in users
                             where c1.Eq()
                             select u).ToList();

                //在Linq查询中应用多表关联和查询对象。返回匿名类型
                var query1 = (from u in session.CreateLinq<AdminUser1>()
                              join c in session.CreateLinq<Class1>() on u.CID equals c.ID
                              join d in session.CreateLinq<DList1>() on u.CID equals d.ID
                              where cdc.Eq()
                              orderby u.Age
                              select new
                              {
                                  u.Age,
                                  u.ID,
                                  u.NameA,
                                  ClassName = c.Name,
                                  DName = d.Name,
                                  IsDelete = u.IsDel
                              }).ToList();
                
                #endregion

                //var data = query1.Name;
            }

         


            Console.ReadLine();

         

        }


    }


    public class DAL1
    {
        public DataSet GetAdminUserById(string id, string name)
        {
           var commandInfo = DbSession.Default.GetCommandInfo(MethodBase.GetCurrentMethod().Name);
           return commandInfo.QueryData(id, name);
        }

    }

    public class SModel
    {
        public AdminUser1 User { set; get; }
        public Class1 Class { set; get; }
        public DList1 D { set; get; }

    }


    public class DUSER
    {
        public string ID { set; get; }
        public string NAME { set; get; }
        public string COLUMN1 { set; get; }
        public string COLUMN2 { set; get; }
    }


    public partial class AdminUser1 : DbObject
    {
        static AdminUser1()
        {
            #region 数据元数据初始化
            
            TableNameProperty.AddOwner(typeof(AdminUser1), "AdminUser");

            KeysProperty.AddOwner(typeof(AdminUser1),new string[]{"ID"});

            #endregion
        }


        #region 数据字段属性
        

        public static ExtendProperty IDProperty =
            ExtendProperty.RegisterProperty("ID", typeof(string), typeof(AdminUser1),
            new DbMetaData() { FieldName = "ID", IsKey = true, IDentity = false, Type = DbType.StringFixedLength });

        public static ExtendProperty NameProperty =
            ExtendProperty.RegisterProperty("NameA", typeof(string), typeof(AdminUser1),
            new DbMetaData() { FieldName = "Name", Type = DbType.AnsiString });

        public static ExtendProperty AgeProperty =
            ExtendProperty.RegisterProperty("Age", typeof(decimal?), typeof(AdminUser1),
            new DbMetaData() { FieldName = "Age", Type = DbType.Decimal });

        public static ExtendProperty DateProperty =
            ExtendProperty.RegisterProperty("Date", typeof(DateTime?), typeof(AdminUser1),
            new DbMetaData() { FieldName = "DateTime", Type = DbType.DateTime });

        public static ExtendProperty ClassProperty =
          ExtendProperty.RegisterProperty("CID", typeof(string), typeof(AdminUser1),
          new DbMetaData() { FieldName = "CID", Type = DbType.String });

        public static ExtendProperty IsDelProperty =
         ExtendProperty.RegisterProperty("IsDel", typeof(bool?), typeof(AdminUser1),
         new DbMetaData() { FieldName = "isDel", RemoveAble = true ,Type = DbType.String });


        #endregion

        #region CLR字段属性
       
        public string ID
        {
            set
            {
                this.SetValue(IDProperty, value);
            }
            get
            {
                return (string)this.GetValue(IDProperty);
            }
        }
        public string NameA
        {
            set
            {
                this.SetValue(NameProperty, value);
            }
            get
            {
                return (string)this.GetValue(NameProperty);
            }
        }
        public decimal? Age
        {
            set
            {
                this.SetValue(AgeProperty, value);
            }
            get
            {
                return (decimal?)this.GetValue(AgeProperty);
            }
        }
        public DateTime? Date
        {
            set
            {
                this.SetValue(DateProperty, value);
            }
            get
            {
                return (DateTime?)this.GetValue(DateProperty);
            }
        }

        public string CID
        {
            set
            {
                this.SetValue(ClassProperty, value);
            }
            get
            {
                return (string)this.GetValue(ClassProperty);
            }
        }

        public bool? IsDel
        {
            set
            {
                this.SetValue(IsDelProperty, value);
            }
            get
            {
                return (bool?)this.GetValue(IsDelProperty);
            }

        }
        #endregion


    }


    public partial class Class1 : DbObject
    {
        static Class1()
        {
            #region 数据元数据初始化
            TableNameProperty.AddOwner(typeof(Class1),"Class");
            KeysProperty.AddOwner(typeof(Class1),new string[]{ "ID" });
            #endregion
        }

        #region 数据字段属性


        public static ExtendProperty IDProperty =
            ExtendProperty.RegisterProperty("ID", typeof(string), typeof(Class1),
            new DbMetaData() { FieldName = "ID", IsKey = true, IDentity = false, Type = DbType.StringFixedLength });

        public static ExtendProperty NameProperty =
            ExtendProperty.RegisterProperty("Name", typeof(string), typeof(Class1),
            new DbMetaData() { FieldName = "Name", Type = DbType.AnsiString });

    
        #endregion

        #region CLR字段属性

        public string ID
        {
            set
            {
                this.SetValue(IDProperty, value);
            }
            get
            {
                return (string)this.GetValue(IDProperty);
            }
        }
        public string Name
        {
            set
            {
                this.SetValue(NameProperty, value);
            }
            get
            {
                return (string)this.GetValue(NameProperty);
            }
        }
    
        #endregion

    }


    public partial class DList1 : DbObject
    {
        static DList1()
        {
            #region 数据元数据初始化

            //TableNameProperty.MetaData = new MetaData() { Tag = "Class" };
            TableNameProperty.AddOwner(typeof(DList1), "DList");
            KeysProperty.AddOwner(typeof(DList1), new string[] { "ID" });

            #endregion
        }

        #region 数据字段属性


        public static ExtendProperty IDProperty =
            ExtendProperty.RegisterProperty("ID", typeof(string), typeof(DList1),
            new DbMetaData() { FieldName = "ID", IsKey = true, IDentity = false, Type = DbType.StringFixedLength });

        public static ExtendProperty NameProperty =
            ExtendProperty.RegisterProperty("Name", typeof(string), typeof(DList1),
            new DbMetaData() { FieldName = "Name", Type = DbType.AnsiString });


        #endregion

        #region CLR字段属性

        public string ID
        {
            set
            {
                this.SetValue(IDProperty, value);
            }
            get
            {
                return (string)this.GetValue(IDProperty);
            }
        }
        public string Name
        {
            set
            {
                this.SetValue(NameProperty, value);
            }
            get
            {
                return (string)this.GetValue(NameProperty);
            }
        }

        #endregion


        
    }

   
}
